package com.bocai.yuweiwangluo.net;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.bocai.yuweiwangluo.base.BaseView;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by win7 on 2015/12/3.
 */
public class MyOkHttpClient implements MyOkHttpWay
{
    public static Context context;
    private String content;
    public  static BaseView baseView;
    public MyOkHttpClient(Context context,BaseView baseView) {
        this.context = context;
        this.baseView=baseView;
    }


    private static final OkHttpClient okHttpClient = new OkHttpClient();
    //参数类型
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");

    public static void postOkHttp(RequestBody requestBody, String url, final Handler handler) {
        //baseView.showLoading();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = null;
        okHttpClient.interceptors().add(new ApiHeaders());
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
               // baseView.hideLoading();
                Message msg = Message.obtain();
                msg.obj = "无法连接到网络，请检查网络配置";
                msg.what = 2;
                handler.sendMessage(msg);
                e.printStackTrace();
            }

            @Override
            public void onResponse(Response response) throws IOException {
               // baseView.hideLoading();
                String result = response.body().string();
                Message msg = Message.obtain();
                msg.obj = result;
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });
    }
    /**
     * 获取验证码
     * @param url
     * @param mobile
     * @param handler
     */
    @Override
    public void getCode(String url, String mobile, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    /**
     * 验证验证码
     * @param url
     * @param mobile
     * @param verify_code
     * @param handler
     */
    @Override
    public void verificationCode(String url, String mobile, String verify_code, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .add("verify_code", verify_code)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    /**
     * 向服务器发送验证码，将验证码保存在服务器数据库中
     * @param url
     * @param mobile
     * @param verify_code
     * @param handler
     */
//    @Override
//    public void saveCode(String url, String mobile, String verify_code, Handler handler) {
//        RequestBody requestBody = new FormEncodingBuilder()
//                .add("mobile", mobile)
//                .add("verify_code", verify_code)
//                .build();
//        postOkHttp(requestBody, url, handler);
//    }

    /**
     * 用户注册
     * @param url
     * @param mobile
     * @param password
     * @param nickname
     * @param handler
     */
    @Override
    public void register(String url, String mobile, String password, String nickname,String number, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile",mobile)
                .add("password", password)
                .add("nickname", nickname)
                .add("number",nickname)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    /**
     * 用户登录
     * @param url
     * @param mobile
     * @param password
     * @param handler
     */
    @Override
    public void login(String url, String mobile, String password, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .add("password", password)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void changePsd(String url, String mobile, String password,String oldpwd, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .add("password", password)
                .add("oldpwd", oldpwd)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void forgetPwd(String url, String mobile, String password, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .add("password", password)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getGameList(String url, String page, String type_id, String city_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page",page)
                .add("type_id", type_id)
                .add("city_id", city_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getGame(String url, String id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getVenueListHeader(String url, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getVenueList(String url, String page, String type_id, String city_id,String title,String local_x,String local_y,String sort, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page", page)
                .add("type_id", type_id)
                .add("city_id",city_id)
                .add("title",title)
                .add("local_x",local_x)
                .add("local_y",local_y)
                .add("sort",sort)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void publishMatch(String url, String type_id, String city_id, String num,
                               String match_type, String content, String stime, String etime,
                               String addr, String usr_id, String price, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("type_id", type_id)
                .add("city_id", city_id)
                .add("num",num)
                .add("match_type",match_type)
                .add("content",content)
                .add("stime",stime)
                .add("etime",etime)
                .add("addr", addr)
                .add("usr_id",usr_id)
                .add("price",price)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getYuezhanListHeader(String url, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getYuezhanList(String url, String page, String type_id, String city_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page",page)
                .add("type_id", type_id)
                .add("city_id",city_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void uploadPhoto(String url, String pic, String ext, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("pic", pic)
                .add("ext", ext)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void updataPhoto(String url, String usr_id, String icon, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id",usr_id)
                .add("icon", icon)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getMyGame(String url, String page, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page",page)
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getMyMatch(String url, String page, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page",page)
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void joinGame(String url, String usr_id, String game_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id",usr_id)
                .add("game_id", game_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void joinMatch(String url, String usr_id, String match_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id",usr_id)
                .add("match_id", match_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getbbsHome(String url, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getbbsList(String url, String type_id, String page, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("type_id", type_id)
                .add("page", page)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getbbsDetail(String url, String id, String page, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .add("page", page)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void addbbs(String url, String usr_id, String title, String content, String type_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id",usr_id)
                .add("title", title)
                .add("content", content)
                .add("type_id", type_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void replybbs(String url, String usr_id, String content, String bbs_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id",usr_id)
                .add("content", content)
                .add("bbs_id", bbs_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getbbsType(String url, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void updataNickName(String url, String id, String nickname, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .add("nickname", nickname)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void updatainfo(String url, String id,String sex,String area,String type_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .add("sex", sex)
                .add("area", area)
                .add("type_id", type_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getInfo(String url, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getMyVenue(String url, String page, String usr_id,String local_x,String local_y, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page", page)
                .add("usr_id", usr_id)
                .add("local_x", local_x)
                .add("local_y", local_y)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getMatchJoin(String url, String id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void addVenue(String url, String place_info_id, String order_day, String order_time, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("place_info_id", place_info_id)
                .add("order_day", order_day)
                .add("order_time", order_time)
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);

    }

    @Override
    public void getMyCoupon(String url, String usr_id, String page,String is_exp ,Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .add("page", page)
                .add("is_exp",is_exp)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getHomeData(String url,String local_x,String local_y, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("local_x", local_x)
                .add("local_y", local_y)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getOrder(String url, String usr_id, String id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .add("id", id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void updataPhone(String url, String mobile, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("mobile", mobile)
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void updataCoupon(String url, String usr_id, String coupon_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .add("coupon_id", coupon_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getMessageList(String url, String page, String usr_id,String is_read, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("page", page)
                .add("usr_id", usr_id)
                .add("is_read", is_read)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void setMessage(String url, String id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("id", id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void setAllMessage(String url, String usr_id, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void getCodeIntro(String url, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .build();
        postOkHttp(requestBody, url, handler);
    }

    @Override
    public void venueComment(String url, String usr_id, String place_id, String content, Handler handler) {
        RequestBody requestBody = new FormEncodingBuilder()
                .add("usr_id", usr_id)
                .add("place_id", place_id)
                .add("content", content)
                .build();
        postOkHttp(requestBody, url, handler);
    }


}
